package com.xt.purchasingsystem.shiro;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xt.purchasingsystem.mapper.SysUsersMapper;
import com.xt.purchasingsystem.model.SysUsers;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;


public class ErpRealm extends AuthorizingRealm {
    @Autowired
    private SysUsersMapper sysUsersMapper;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String userName = token.getPrincipal().toString();

        SysUsers user = sysUsersMapper.selectOne(new QueryWrapper<SysUsers>().eq("login_id",userName));
        //将用户信息封装在 AuthenticationInfo中
        return new SimpleAuthenticationInfo(user.getLoginId(), //数据库中的用户名
                user.getPassword(), //数据库中的密码
                ByteSource.Util.bytes(user.getSalt())  //盐
                , this.getName());

    }
}
